//
//  LBMTextAttachment.swift
//  EmoticonKeyboard
//
//  Created by 刘保满 on 16/6/2.
//  Copyright © 2016年 itheima. All rights reserved.
// 自定义附件类,封装方法

import UIKit

class LBMTextAttachment: NSTextAttachment {
    
    //表情的中文
    var chs: String?

    //将表情图片添加到属性文本中
    class func emoticonImageToImageText(em: LBMEmoticon,font: UIFont) -> NSAttributedString {
        
        //1.获取附件对象
        let attachment = LBMTextAttachment()
        //2.给附件对象添加图片
        attachment.image = UIImage(contentsOfFile: em.imagePath ?? "")
        
        attachment.chs = em.chs
        
        let lineHeight = font.lineHeight
        
        attachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        
        //3.通过附件对象,实例化属性字符串
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        
        //4.给属性文本添加font属性
        imageText.addAttributes([NSFontAttributeName : font], range: NSMakeRange(0, 1))
        
        return imageText
    }
    
    
    
    
}
